﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DBUtility;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class WxUserInfoDAL
    {

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(wx_userinfo model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@openid",model.openid),
                new MySqlParameter("@nickname",model.nickname),
                new MySqlParameter("@city",model.city),
                new MySqlParameter("@country",model.country),
                new MySqlParameter("@province",model.province),
                new MySqlParameter("@sex",model.sex),
                new MySqlParameter("@language",model.language),
                new MySqlParameter("@headimgurl",model.headimgurl),
                new MySqlParameter("@subscribe_time",model.subscribe_time),
                new MySqlParameter("@subscribe",model.subscribe),
                new MySqlParameter("@remark",model.remark),
                new MySqlParameter("@groupid",model.groupid)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("INSERT INTO wx_userinfo(`openid`,`nickname`,`sex`,`city`,`country`,`province`,`language`,`headimgurl`,`subscribe_time`,`subscribe`,`remark`,`groupid`) VALUES (@openid, @nickname, @sex, @city, @country, @province, @language, @headimgurl, subscribe_time, @subscribe, @remark, @groupid)", para);
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string columns, string where)
        {
            string sql = string.Format("select {0} from wx_userinfo where 1=1 {1}", columns, where);
            return DBUtility.MySqlHelper.ExecuteDynamic(sql);
        }

        /// <summary>
        /// 是否存在openid（true表示存在）
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public bool IsExistOpenID(string openid)
        {
            List<dynamic> list = GetList("openid", " and openid='" + openid + "'");
            if (list.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新是否关注标识
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="subscribe"></param>
        /// <returns></returns>
        public int UpdateSubscribe(string openid,int subscribe)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@openid",openid),
                new MySqlParameter("@subscribe",subscribe) 
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("UPDATE wx_userinfo SET wx_userinfo.`subscribe`=@subscribe WHERE openid=@openid",para);
        }


    }
}
